<!--
 * @Author: H.
 * @Date: 2021-12-07 17:31:54
 * @LastEditTime: 2021-12-14 13:52:03
 * @Description:
-->

<template>
  <div class="page-content">
    <hy-table
      :tableData="dataList"
      v-bind="contentTableConfig"
      :listCount="dataCount"
      v-model:page="pageInfo"
      @selectionChange="selectionChange"
    >
      <!-- header中的插槽 -->
      <template #headerHandle>
        <el-button
          @click="handleNewClick"
          v-if="isCreate && contentTableConfig.btnContent"
          >{{ contentTableConfig.btnContent }}</el-button
        >
      </template>

      <!-- 列中的插槽 -->
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="{ row }">
        <span>{{ $filters.formatTime(row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div>
          <el-button
            icon="i-edit"
            size="mini"
            type="text"
            @click="handleEditClick(scope.row)"
            v-if="isUpdate"
            >编辑</el-button
          >
          <el-button
            icon="i-delete"
            @click="handleDeleteClick(scope.row)"
            size="mini"
            type="text"
            v-if="isDelete"
            >删除</el-button
          >
        </div>
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row" />
        </template>
      </template>
    </hy-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from '@/store'
import hyTable from '@/base-ui/table'
import { usePermission } from '@/hooks/usePermission'
export default defineComponent({
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const store = useStore()

    // 获取操作的权限
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')

    // 双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => getPageData())

    // 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('system/getPageListActions', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    // 从vuex获取数据
    const dataList = computed(() =>
      store.getters['system/pageListData'](props.pageName)
    )
    const dataCount = computed(() =>
      store.getters['system/pageListCount'](props.pageName)
    )
    const selectionChange = (value: any) => {
      console.log(value, 'user')
    }

    // 获取其他的动态插槽名称
    const otherPropSlots = props.contentTableConfig?.propList.filter(
      (item: any) => {
        if (
          item.slotName === 'createAt' ||
          item.slotName === 'updateAt' ||
          item.slotName === 'handler'
        ) {
          return false
        }
        return true
      }
    )

    // 新增/编辑/删除
    const handleDeleteClick = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }

    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }

    const handleNewClick = () => {
      emit('newBtnClick')
    }
    return {
      selectionChange,
      dataList,
      getPageData,
      dataCount,
      pageInfo,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      isQuery,
      handleDeleteClick,
      handleEditClick,
      handleNewClick
    }
  },
  props: {
    contentTableConfig: {
      type: Object,
      require: true
    },
    pageName: {
      type: String,
      default: '',
      require: true
    }
  },
  components: {
    hyTable
  }
})
</script>

<style lang="scss" scoped>
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
